package test.generic;

import com.alibaba.fastjson.JSONObject;

import java.util.Date;

/**
 * 泛型类
 * 比如：使用泛型来对类的构造函数的参数个数进行限定
 */
public class GenericClass {

    public static void main(String[] args) {
        TwoTuple<Integer, String> twoT = new TwoTuple<Integer, String>(99, "掌上洪城");
        System.out.println(twoT);

        String s = TwoTuple.function("aaaa");

        ThreeTuple<Integer, String, Date> threeT = new ThreeTuple<Integer, String, Date>(99, "掌上洪城", new Date());
        System.out.println(threeT);

        JSONObject e = new JSONObject();
        e.put("mid","ssdfasf");
        System.out.println(e.getLong("mid"));

    }
}

class TwoTuple<A, B> {
    public final A first;
    public final B second;

    public TwoTuple(A a, B b) {
        first = a;
        second = b;
    }

    public void function2(A t) {
        System.out.println("function:" + t);
    }

    /**
     * 静态方法上的泛型：静态方法无法访问类上定义的泛型。如果静态方法操作的引用数据类型不确定的时候，必须要将泛型定义在方法上
     */
    public static <A> A function(A t) {
        System.out.println("function:" + t);
        return t;
    }

    @Override
    public String toString() {
        return "(" + first + "," + second + ")";
    }
}

class ThreeTuple<A, B, C> extends TwoTuple<A, B> {
    private final C three;

    public ThreeTuple(A a, B b, C c) {
        super(a, b);
        three = c;
    }

    @Override
    public String toString() {
        return "(" + first + "," + second + "," + three + ")";
    }
}

